package com.shine.icis.action;

import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.cotrun.web.upload.FileDispatcher;
import com.shine.icis.blo.IDataCheckService;
import com.shine.icis.blo.IDataSubmitService;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dto.ProgressStatus;
import com.shine.icis.hbean.DataCheckDef;
import com.shine.icis.hbean.DataCheckR09;

public class DataCheck09Action extends BaseAction {

	/**
	 * 资金账户完整性核对
	 * @author zhangxr
	 */
	private static final long serialVersionUID = 7638121893867812633L;
	
	private IDataCheckService dataCheckService;
	private IDataSubmitService dataSubmitService;
	
	// input param
	private int pageNo;
	private int pageSize;
	private String fundAcct;//客户资金账户账号
	private String compareFlag;//是否一致
	private String expType;//导出方式
	//output value
	private ListPage<DataCheckR09> checkR09Page;//R10查询结果
	private List<DataCheckDef> dataCheckDefs;
	private String currentBusiDate;//当前交易日期
	private int jobStatus;//数据核对的状态：-2未就绪；-1等待；0运行中；1结束；2结束但TS处于运行中
	private String downloadUrl;//下载地址
	
	public void setDataCheckService(IDataCheckService dataCheckService) {
		this.dataCheckService = dataCheckService;
	}
	public void setDataSubmitService(IDataSubmitService dataSubmitService) {
		this.dataSubmitService = dataSubmitService;
	}
	
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setFundAcct(String fundAcct) {
		this.fundAcct = fundAcct;
	}
	
	public void setCompareFlag(String compareFlag) {
		this.compareFlag = compareFlag;
	}
	
	public void setExpType(String expType) {
		this.expType = expType;
	}
	
	public ListPage<DataCheckR09> getCheckR09Page() {
		return checkR09Page;
	}
	public List<DataCheckDef> getDataCheckDefs() {
		return dataCheckDefs;
	}
	
	public String getCurrentBusiDate() {
		return currentBusiDate;
	}
	
	public int getJobStatus() {
		return jobStatus;
	}
	
	public String getDownloadUrl() {
		return downloadUrl;
	}
	/**
	 * 进入查询页面
	 * @return
	 */
	public String initQueryCheckR09() {
		dataCheckDefs = dataCheckService.getDataCheckDefs(DataCheckDef.ON);
		currentBusiDate = dataSubmitService.getBusiDate();
		ProgressStatus proSta = dataSubmitService.getProgressStatus();
		jobStatus = proSta.getDataCheckStatus();
		return success();
	}
	/**
	 * 查询
	 * */
	public String queryCheckR09() {
		QueryParameters qp = new QueryParameters();
		
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		//排序信息
		qp.addOrder("fundAcct",false);
		qp.addOrder("compareDesc",false);
		qp.addOrder("id", true);
		
		//参数
		if(fundAcct != null && !fundAcct.trim().equals("")){
			qp.addParameter("fundAcct", fundAcct);
		}
		if(compareFlag != null && !compareFlag.trim().equals("")){
			qp.addParameter("compareFlag",compareFlag);
		}
		try {
			checkR09Page = dataCheckService.getDataCheckR09Page(qp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 导出Excel
	 */
	public String exportCheckR09() {
		QueryParameters qp = new QueryParameters();
		//排序信息
		qp.addOrder("fundAcct",false);
		qp.addOrder("compareDesc",false);
		qp.addOrder("id", true);
		
		//参数
		if(fundAcct != null && !fundAcct.trim().equals("")){
			qp.addParameter("fundAcct", fundAcct);
		}
		if(compareFlag != null && !compareFlag.trim().equals("")){
			qp.addParameter("compareFlag",compareFlag);
		}
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			String temFileName = dataCheckService.csExpDataCheckR09(qp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}
}
